layui.config({
    base : "/js/"
}).extend({
    "mframe" : "mframe",
    "treetable":"treetable"
})
layui.use(['form','layer','laydate','table','mframe',"treetable"],function(){
    var form = layui.form,
    	treetable = layui.treetable,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laydate = layui.laydate,
        mframe = layui.mframe,
        table = layui.table;
    var datas = mframe.Dic.getDicList("sys_normal_disable");
    var prefix = ctx + "system/dept"
    var data = [];
    
    $.ajax( {
        url: prefix+"/list",
        type: "get",
        async: false,
        dataType: "json",
        success: function(result) {
        	data = result;
        }
    })
    
    treetable.render({
		elem: '#layui-table',
		data: data,
		field: 'deptName',
		is_checkbox: false,
		space: 4,
		cols: [
			{
				field: 'deptName',
				title: '部门名称',
			},
			{
				field: 'orderNum',
				title: '排序',
			},
			{
				title: '状态',
				template:function(e){
					debugger;
		        	return mframe.Dic.getDictValue(datas,e.status);
		        }
			},
			{
				field: 'createTime',
				title: '创建时间',
			},
			{
				title: '操作',
				template: function(item){
					var tem = [];
					tem.push('<a class="layui-btn layui-btn-sm layui-btn-normal" lay-filter="add">新增</a>');
					tem.push('<a class="layui-btn layui-btn-sm" lay-filter="edit">编辑</a>');
					tem.push('<a class="layui-btn layui-btn-sm layui-btn-danger" lay-filter="del">删除</a>');
					return tem.join();
				},
			},
		]
	});
    treetable.all('down');

    $(".add").click(function(){
    	var url = prefix + "/add"+"/100"
    	mframe.addOrUpdate(url,"添加菜单");
    })

    //批量删除
    $(".delAll_btn").click(function(){
    	var url = prefix + "/remove"
    	mframe.removeAll(table,url,tableIns,"roleId");
    })

    //列表操作
    treetable.on('treetable(add)',function(data){
    	debugger;
    	var url = prefix + "/add/"+data.item.deptId
    	mframe.addOrUpdate(url,"添加菜单");
	})
	
	treetable.on('treetable(edit)',function(data){
    	var url = prefix + "/edit/"+data.item.deptId
    	mframe.addOrUpdate(url,"编辑菜单");
	})
	
	treetable.on('treetable(del)',function(data){
		var url = prefix + "/remove/"+data.item.deptId;
		layer.confirm('确定删除此信息？',{icon:3, title:'提示信息'},function(index){
        	var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
    	    $.ajax( {
    	        url: url,
    	        type: "post",
    	        dataType: "json",
    	        success: function(result) {
    	        	layer.close(index);
	        		layer.msg(result.msg);
	        		location.reload();
    	        }
    	    })
        });
	})
})